﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Frame.Lib.Common;

namespace Frame.Lib.Web
{
    public delegate void PageCountChangeHandar(object sender);
    public enum SortDirection
    {
        asc,
        desc,
    }
    public class PagerSortSetting
    {
        public event PageCountChangeHandar PageCountChanged;

        private int _recordCount;
        private object _output;
        private object _input;

        private const string SORT_DIRECTION = "SortDirection";
        private const string SORT_EXPRESSION = "SortExpression";
        private const string PAGE_COUNT = "PageCount";
        private const string PAGE_INDEX = "PageIndex";

        public int SkipCount
        {
            get
            {
                return (PageIndex - 1) * PageSize;
            }
        }

        public SortDirection SortDirection
        {
            get
            {
                if (_viewState[SORT_DIRECTION] == null)
                {
                    _viewState[SORT_DIRECTION] = SortDirection.asc;
                }

                SortDirection direction = (SortDirection)_viewState[SORT_DIRECTION];


                return direction;
            }
            set
            {
                _viewState[SORT_DIRECTION] = value;
            }
        }

        public string SortExpression
        {
            get
            {
                return (string)_viewState[SORT_EXPRESSION];
            }
            set
            {
                _viewState[SORT_EXPRESSION] = value;
            }
        }

        /// <summary>
        /// 从外往里带的一些条件
        /// </summary>
        public object Input
        {
            get { return _input; }
            set { _input = value; }
        }

        /// <summary>
        /// 从底层往外带的一些数据
        /// </summary>
        public object Output
        {
            get { return _output; }
            set { _output = value; }
        }



        private global::System.Web.UI.StateBag _viewState;


        public PagerSortSetting(global::System.Web.UI.StateBag viewState)
        {
            this._viewState = viewState;
            PageSize = AppSetting.PageSize;
        }

        /// <summary>
        /// 每页的大小
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }


        public int PageIndex
        {
            get
            {
                if (_viewState[PAGE_INDEX] == null)
                {
                    _viewState[PAGE_INDEX] = 1;
                }

                if ((int)_viewState[PAGE_INDEX] > PageCount)
                {
                    if (PageCount > 0)
                    {
                        _viewState[PAGE_INDEX] = PageCount;
                    }
                    else
                    {
                        _viewState[PAGE_INDEX] = 1;
                    }
                }

                return (int)_viewState[PAGE_INDEX];
            }
            set
            {
                _viewState[PAGE_INDEX] = value;
            }
        }

        public int RecordCount
        {
            get
            {
                return _recordCount;
            }
            set
            {
                _recordCount = value;

                //把总页数放ViewState便于“末页” 调用
                int pageCount = value / PageSize;
                if (value % PageSize != 0)
                {
                    pageCount++;
                }
                _viewState[PAGE_COUNT] = pageCount;

                PageCountChanged(this);

            }

        }

        public int PageCount
        {
            get
            {
                return (int)_viewState[PAGE_COUNT];
            }
        }

        public System.Data.DataView GetDataView(System.Data.DataTable dt)
        {
            RecordCount = dt.Rows.Count;
            DataView dv = new DataView(dt);
            if (RecordCount > 0)
            {
                if (SortExpression != null && SortExpression != "")
                {
                    dv.Sort = SortExpression + " " + SortDirection;
                }
                int end = SkipCount + PageSize;
                if (end > dt.Rows.Count)
                {
                    end = dt.Rows.Count;
                }
                StringBuilder ids = new StringBuilder();
                for (int i = SkipCount; i < end; i++)
                {
                    ids.Append("," + dv[i][0]);
                }
                if (ids.Length > 0)
                {
                    dv.RowFilter = dt.Columns[0].ColumnName + " in (" + ids.ToString().Trim(',') + ")";
                }
            }
            return dv;
        }

        public IEnumerable<Info> GetList<Info>(IEnumerable<Info> list)
        {
            RecordCount = list.Count();
            if (RecordCount > 0)
            {
                if (!string.IsNullOrEmpty(SortExpression))
                {
                    if (SortDirection == SortDirection.asc)
                    {
                        list = list.OrderBy(t => t.GetType().GetProperty(SortExpression).GetValue(t, null));
                    }
                    else
                    {
                        list = list.OrderByDescending(t => this.GetType().GetProperty(SortExpression).GetValue(t, null));
                    }
                }
                list = list.Skip(SkipCount).Take(PageSize);
            }
            return list;
        }



    }
}
